#import "TablesManager.h"
#import "BillsManager.h"
@interface TablesManager ()

@property NSMutableArray* tablesArray;
@property NSTimer* updateTimer;

@end

static NSString* kTablesUrl = @"http://78.90.20.186:8080/ProjectRestaurant/getTables";

@implementation TablesManager

+ (id)sharedTables {
    static TablesManager *sharedTables = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedTables = [[self alloc] init];
    });
    return sharedTables;
}

- (instancetype)init{
    if (self = [super init]) {
        self.tablesArray = [[NSMutableArray alloc] init];
        self.updateTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(update) userInfo:nil repeats:YES];
    }
    return self;
}

- (void) selectTableWithIndex:(NSInteger) index{
    if (index < [self count] && index > -1) {
        self.selectedTable = self.tablesArray[index];
    }
    else{
        self.selectedTable = nil;
    }
}

- (NSInteger) getNumberForTableAtIndex:(NSInteger) index{
    if (index < [self count] && index > -1) {
        return [self.tablesArray[index] tableNumber];
    }
    else return -1;
}

- (NSInteger) getStatusForTableAtIndex:(NSInteger) index{
    if (index < [self count] && index > -1) {
        return [self.tablesArray[index] status];
    }
    else return -1;
}

- (BOOL) pendingOrderForTableAtIndex:(NSInteger) index{
    if (index < [self count] && index > -1){
        return [self.tablesArray[index] pendingOrder];
    }
    else return NO;
}

- (BOOL) messageForTableAtIndex:(NSInteger) index{
    if (index < [self count] && index > -1){
        return [self.tablesArray[index] hasMessage];
    }
    else return NO;
}

- (NSInteger) count{
    return [self.tablesArray count];
}

- (Table*) tableForIndex:(NSInteger) index{
    if (index < [self count] && index > -1){
        return self.tablesArray[index];
    }
    else return nil;
}


//Download Tables info from server
-(void) update{
    NSURLSession* session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:kTablesUrl]];
    
    NSURLSessionDataTask* downloadJSONTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSError* mistake = nil;
        ;
        NSArray* jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&mistake];
        if (mistake != nil) {
            NSLog(@"Error : %@", [mistake localizedDescription]);
        }
        else {
            [self.tablesArray removeAllObjects];
//           NSLog(@"Updating Tables");
            for (NSDictionary* item in jsonArray) {
                Table* table = [[Table alloc] initWithNumber:[item[@"tableId"] integerValue]];
                table.status = [item[@"active"] boolValue];
                table.pendingOrder = [item[@"hasOrder"] boolValue];
                table.hasMessage = [item[@"hasMessage"] boolValue];
                [self.tablesArray addObject:table];
            }
            [[NSNotificationCenter defaultCenter] postNotificationName:@"table" object:self userInfo:nil];
        }
        
        
    }];
    [downloadJSONTask resume];
}



@end
